package com.baidu.process.ngd.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.process.chat.core.web.bean.IMRequestServer;
import com.baidu.process.config.OkHttpConfiguration;
import com.baidu.process.chat.parser.FirstParseNgdResult;
import com.baidu.process.ngd.bean.NGDReqBody;
import com.baidu.process.ngd.bean.NGDResBody;
import com.baidu.process.ngd.bean.NGDStartResBody;
import com.baidu.process.ngd.service.NGDService;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class NGDServiceImpl implements NGDService {

    private static final Logger logger= LoggerFactory.getLogger(NGDServiceImpl.class);

    @Value("${ngd.query.url}")
    private String queryNgdUrl;

    @Value("${ngd.start.url}")
    private String startNgdUrl;

    @Autowired
    OkHttpConfiguration okHttpConfiguration;

    @Autowired(required = false)
    FirstParseNgdResult firstParseNgdResult;

    @Autowired
    RedisTemplate<String,String> redisTemplate;


    @Override
    public NGDResBody query(IMRequestServer imRequestServer,NGDReqBody ngdReqBody) {
        String body= JSON.toJSONString(ngdReqBody);
        String botId=imRequestServer.getBotToken();
        MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON,body);
        try {
            long start=System.currentTimeMillis();
            OkHttpClient okHttpClient = okHttpConfiguration.okHttpClient();
            Request request = new Request.Builder()
                    .url(queryNgdUrl).post(requestBody).
                            addHeader("Authorization",botId).addHeader("Content-Type","application/json").build();
            Response response = okHttpClient.newCall(request).execute();
            String jsonRes = response.body().string();
            logger.info("[sessionid:]{}NGD返回报文:{}",imRequestServer.getSessionId(),jsonRes);
            NGDResBody ngdResult= JSONObject.parseObject(jsonRes, NGDResBody.class);
            return ngdResult;
        }catch (IOException e){
            logger.info("请求NGD报文:{}------输出异常:{}",body,e.getMessage());
        }
        return null;
    }

    @Override
    public NGDStartResBody start(IMRequestServer imRequestServer, NGDReqBody ngdReqBody) {
        String body= JSON.toJSONString(ngdReqBody);
        MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
        String botId=imRequestServer.getBotToken();
        RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON,body);
        try {
            long start=System.currentTimeMillis();

            OkHttpClient okHttpClient = okHttpConfiguration.okHttpClient();


            Request request = new Request.Builder()
                    .url(startNgdUrl).post(requestBody).
                            addHeader("Authorization",botId).addHeader("Content-Type","application/json").build();

            Response response = okHttpClient.newCall(request).execute();
            String jsonRes = response.body().string();
            logger.info("[sessionid:]{}NGD返回报文:{}",imRequestServer.getSessionId(),jsonRes);
            NGDStartResBody ngdResult= JSONObject.parseObject(jsonRes, NGDStartResBody.class);

            return ngdResult;
        }catch (IOException e){
            logger.info("请求NGD报文:{}------输出异常:{}",body,e.getMessage());
        }
        return null;
    }

}
